﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Speedy
{
    public partial class frmArac : XtraForm
    {
        public frmArac()
        {
            InitializeComponent();
        }

        private void frmArac_Load(object sender, EventArgs e)
        {
            Liste();

            dbMerchant pls = new dbMerchant();
            pls.name.Alias = "NAME";
            pls.id.Alias = "ID";
            DataTable dt = pls.AllData();
            if (dt.Rows.Count > 0)
            {
                cmbPlasiyer.DisplayMember = "NAME";
                cmbPlasiyer.ValueMember = "ID";
                cmbPlasiyer.DataSource = dt;
                cmbPlasiyer.SelectedIndex = 0;
            }
        }

        private void Liste()
        {
            dbVehicle arac = new dbVehicle();
            arac.code.Alias = "Araç Kodu";
            arac.name.Alias = "Araç Adı";
            gridControl1.DataSource = arac.AllData();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            dbVehicle arac = new dbVehicle();
            arac.code.SetValue(txtKod.Text);
            arac.name.SetValue(txtAdi.Text);
            arac.merchantId.SetValue(Convert.ToInt32(cmbPlasiyer.SelectedValue));
            arac.Save();
            Liste();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            dbVehicle arac = new dbVehicle();
            arac.code.SetValue(txtKod.Text);
            arac.Remove();
            Liste();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.RowCount > 0 && gridView1.Columns.Count > 0)
                SelectRecord(e.FocusedRowHandle);
        }


        void SelectRecord(int row)
        {
            txtKod.Text = gridView1.GetRowCellValue(row, gridView1.Columns["Araç Kodu"]).ToString();
            txtAdi.Text = gridView1.GetRowCellValue(row, gridView1.Columns["Araç Adı"]).ToString();
            dbVehicle v = new dbVehicle();
            v.code.SetValue(txtKod.Text);
            v.Find();
            cmbPlasiyer.SelectedValue = v.merchantId.Read();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0 && gridView1.Columns.Count > 0)
                SelectRecord(gridView1.FocusedRowHandle);
        }

    }
}
